package com.example.lenovo.zyfypt_221yln.model;

import android.content.Context;


import com.example.lenovo.zyfypt_221yln.iface.GuanzhuListener;
import com.example.lenovo.zyfypt_221yln.iface.Guanzhuiface;
import com.example.lenovo.zyfypt_221yln.service.GuanzhuService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by wangshuo on 2017/5/24.
 */

public class GuanzhuModel implements Guanzhuiface {

    Context context;
    private Retrofit retrofit;
    private String BASEURL
            ="http://amicool.neusoft.edu.cn/";
    //构造函数
    public GuanzhuModel()
    {   //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    public GuanzhuModel(Context context)
    {   //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        this.context = context;
    }
    //关注
    @Override
    public void focus(String mod, int id, String sessionid, final GuanzhuListener listener) {
        //使用Retrofit----2
        GuanzhuService guanzhuService=retrofit.create(GuanzhuService.class);
        Call<String> call=guanzhuService.focus(mod,id,sessionid);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))
                    listener.onResponse("1");//关注失败
                else
                    listener.onResponse("2");//关注成功
                }
                else   listener.onFail("guanzhu onresponse fail");//
            }
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail("关注失败："+t.toString());
            }
        });
    }
    //取消关注
    @Override
    public void unfocus(String mod, int id, String sessionid, final GuanzhuListener listener) {
        //使用Retrofit----2
        GuanzhuService  guanzhuService=retrofit.create(GuanzhuService.class);
        Call<String> call=guanzhuService.unfocus(mod,id,sessionid);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))
                    listener.onResponse("4");//取消关注失败
                else if (response.body().trim().toString().equals("1"))
                    listener.onResponse("5");//取消关注成功
                }
                else   listener.onFail("guanzhu onresponse fail");//uncollect onresponse fail
            }
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail("关注失败："+t.toString());
            }
        });
    }
    //判断是否关注
    @Override
    public void exists(String mod, int id, String sessionid, final GuanzhuListener listener) {
        //使用Retrofit----2
        GuanzhuService  guanzhuService=retrofit.create(GuanzhuService.class);
        Call<String> call=guanzhuService.exists(mod,id,sessionid);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))//已关注
                    listener.onResponse("7");
                else if (response.body().trim().toString().equals("1"))//未关注
                    listener.onResponse("8");
                }
                else   listener.onFail("guanzhu exists onresponse fail");//
            }
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail("判断关注失败："+t.toString());
            }
        });
    }
}
